package com.youfang.sixthmedia.ui.center;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.TextView;

import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.common.YoufangContext;

public class TabCenterActivity extends TabActivity implements
		OnCheckedChangeListener {

	private TabHost mHost;
	int[] rbsid = new int[] { R.id.profileRB, R.id.favRB, R.id.grouponRB };
	int stateRID = R.id.friendRB;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_center_activity);

		initRadios();
		setupIntent();

		((RadioButton) findViewById(R.id.friendRB)).performClick();

	}

	// /////////信息提示/////////////////

	/**
	 * 初始化底部按钮
	 */

	private void initRadios() {

		final RadioButton profileRB = ((RadioButton) findViewById(stateRID));
		profileRB.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				for (int id : rbsid) {
					RadioButton curRB = ((RadioButton) findViewById(id));
					curRB.setChecked(false);
				}

				profileRB.setChecked(true);
				mHost.setCurrentTabByTag("friendRB");

			}
		});

		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View buttonView) {
				profileRB.setChecked(false);
				((RadioButton) buttonView).setChecked(true);

				switch (buttonView.getId()) {
				case R.id.profileRB:
					mHost.setCurrentTabByTag("profileRB");
					updateCommonRBState(R.id.profileRB);
					break;

				case R.id.favRB:
					mHost.setCurrentTabByTag("favRB");
					updateCommonRBState(R.id.favRB);
					break;
				case R.id.grouponRB:
					mHost.setCurrentTabByTag("grouponRB");
					updateCommonRBState(R.id.grouponRB);
					break;

				}

			}
		};

		for (int id : rbsid) {
			RadioButton curRB = ((RadioButton) findViewById(id));
			curRB.setOnClickListener(listener);

		}

	}

	private void updateCommonRBState(int curId) {
		for (int id : rbsid) {
			((RadioButton) this.findViewById(id)).setChecked(curId == id);
		}

	}

	/**
	 * 切换模块
	 */
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

		if (isChecked) {

			switch (buttonView.getId()) {
			case R.id.profileRB:
				this.mHost.setCurrentTabByTag("profileRB");
				break;

			case R.id.favRB:
				this.mHost.setCurrentTabByTag("favRB");
				break;

			case R.id.grouponRB:
				this.mHost.setCurrentTabByTag("grouponRB");
				break;

			}

		}
	}

	private void setupIntent() {

		this.mHost = getTabHost();

		TabHost localTabHost = this.mHost;

		localTabHost.addTab(buildTabSpec("profileRB", new Intent(this,
				CProfileActivity.class)));

		localTabHost.addTab(buildTabSpec("friendRB", new Intent(this,
				CBuddyActivity.class)));

		localTabHost.addTab(buildTabSpec("favRB", new Intent(this,
				CFavActivity.class)));

		localTabHost.addTab(buildTabSpec("grouponRB", new Intent(this,
				CGrouponActivity.class)));

	}

	private TabHost.TabSpec buildTabSpec(String tag, final Intent content) {
		return this.mHost
				.newTabSpec(tag)
				.setIndicator("",
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(content);
	}

	private BroadcastReceiver messageReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {

			int userid = intent.getIntExtra("userid", 0);
			if (userid != YoufangContext.getUser().userid) {
				return;
			}

			int count = intent.getIntExtra("data", 0);
			TextView countView = (TextView) findViewById(R.id.status);
			countView.setVisibility(count == 0 ? View.INVISIBLE : View.VISIBLE);
			countView.setText("" + count);

		}
	};

	private void regService() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(YoufangContext.CHAT_UNREAD_MESSAGE_ATION);
		registerReceiver(messageReceiver, filter);
	}

	
	@Override
	protected void onResume() {
		super.onResume();
		regService();
		
	}
	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(messageReceiver);
	}

}
